/*
 * Copyright (C) 2010 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.khstudio.api.dialog;

import android.app.Activity;
import android.app.DialogFragment;
import android.os.Bundle;

public abstract class KhDialog extends DialogFragment {

    public interface KhDialogListener {
        public void onDialogLoaded(int id, KhDialog d);
        public void onDialogUnLoaded(int id);
        public void onPositiveClick(int id);
        public void onNegativeClick(int id);
        public void onCancelClick(int id);
    }

    protected KhDialogListener    mListener;

    protected int                 mId;
    protected String              mTitle;

    public KhDialog() {
        super();

        mId = 0;
        mTitle = null;

        mListener = null;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mId       = getArguments().getInt("id", 0);
        mTitle    = getArguments().getString("title", null);

        if(mTitle != null) {
            setStyle(DialogFragment.STYLE_NORMAL, 0);
        } else {
            setStyle(DialogFragment.STYLE_NO_TITLE, 0);
        }
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        try {
            mListener = (KhDialogListener) activity;
            mListener.onDialogLoaded(mId, this);
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString() + " must implement KhDialogListener");
        }
    }

    @Override
    public void onDetach() {
        super.onDetach();
        mListener.onDialogUnLoaded(mId);
    }
}
